using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class FinishTaskDAL
    {
        public static int Save(FinishTaskInfo info)
        {
            string sql = string.Format("insert into [finishtask]([SID],[UserID],[IsTake],[Num],[CreateDate]) values(@SID,@UserID,@IsTake,@Num,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@SID", info.SID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@IsTake", info.IsTake),
                new SqlParameter("@Num", info.Num),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(FinishTaskInfo info)
        {
            string sql = string.Format("update [finishtask] set [SID]=@SID,[IsTake]=@IsTake where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@SID", info.SID),
                new SqlParameter("@IsTake", info.IsTake),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static FinishTaskInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[finishtask]");
            return SimpleData(dr);
        }
        public static FinishTaskInfo FindOneByUserID(int sid,int userid)
        {
            DataRow dr = CommonDAL.GetDataRow(string.Format("select * from [finishtask] where SID={0} and UserID={1}", sid, userid));
            return SimpleData(dr);
        }
        public static List<FinishTaskInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[finishtask]");
            return ListSimpleData(dt);
        }
        public static List<dynamic> FinishTask(int sid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select [user].HeadImg,[user].NickName,[finishtask].ID,[finishtask].IsTake,[finishtask].Num,[finishtask].CreateDate,(select COUNT(id) from praise_record where SID={0} and ShareUserID=[finishtask].UserID) as LingCount from finishtask left join [user] on finishtask.UserID=[user].ID where SID={0}", sid));
            List<dynamic> list = new List<dynamic>();
            foreach (DataRow row in dt.Rows)
            {
                dynamic obj = new ExpandoObject();
                obj.ID = DataConvert.ToInt(row["ID"]);
                obj.HeadImg = DataConvert.ToString(row["HeadImg"]);
                obj.NickName = DataConvert.ToString(row["NickName"]);
                obj.IsTake = DataConvert.ToString(row["IsTake"]);
                obj.Num = DataConvert.ToString(row["Num"]);
                obj.LingCount = DataConvert.ToInt(row["LingCount"]);
                obj.CreateDate = DataConvert.ToDateTime(row["CreateDate"]).ToString("yyyy-MM-dd HH:mm");
                list.Add(obj);
            }
            return list.OrderByDescending(x=>x.LingCount).ToList();
        }
        
        public static bool IsExist(int sid, int userid)
        {
            int ID = DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select ID from [finishtask] where SID={0} and UserID={1}", sid, userid)));
            return ID > 0 ? true : false;
        }

        public static int MaxNum(int sid)
        {
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select Max(Num) from [finishtask] where SID={0}", sid)));
        }
        public static List<FinishTaskInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [finishtask].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [finishtask].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter.ToString(), "[finishtask]", out count);
            return ListSimpleData(dt);
        }
        public static List<FinishTaskInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [finishtask]"));
            return ListSimpleData(dt);
        }
        private static FinishTaskInfo SimpleData(DataRow dr)
        {
            FinishTaskInfo info = new FinishTaskInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.IsTake = DataConvert.ToInt(dr["IsTake"]);
                info.Num = DataConvert.ToInt(dr["Num"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static FinishTaskInfo AllData(DataRow dr)
        {
            FinishTaskInfo info = new FinishTaskInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.IsTake = DataConvert.ToInt(dr["IsTake"]);
                info.Num = DataConvert.ToInt(dr["Num"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<FinishTaskInfo> ListSimpleData(DataTable dt)
        {
            List<FinishTaskInfo> list = new List<FinishTaskInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<FinishTaskInfo> ListAllData(DataTable dt)
        {
            List<FinishTaskInfo> list = new List<FinishTaskInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

